package com.dhcc.core.modules.system.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dhcc.core.framework.annotion.CacheClear;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.constant.state.PermPrincipalType;
import com.dhcc.core.framework.constant.state.ResourceGroup;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.util.ArrayUtil;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.cache.permission.IPermissionCache;
import com.dhcc.core.modules.system.dao.PermissionMapper;
import com.dhcc.core.modules.system.dao.RoleMapper;
import com.dhcc.core.modules.system.entity.Permission;
import com.dhcc.core.modules.system.entity.Role;
import com.dhcc.core.modules.system.service.IRoleService;

/**
 * 角色服务
 * 
 * @ClassName: RoleServiceImpl
 * @Description: TODO
 * @author: cyf
 * @date: 2018年1月2日 上午10:41:23
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    RoleMapper roleMapper;
    
    @Autowired
    PermissionMapper permissionMapper;

    @Override
    @Transactional(readOnly = false)
    @CacheClear(IPermissionCache.CACHE_NAME)
    public void setAuthority(Long roleId,String resourceGroup, String ids) {

        // 删除该角色所有的权限
        this.permissionMapper.deleteByPrincipalIdAndResGroup(roleId,ResourceGroup.getCodeByName(resourceGroup));

        // 添加新的权限
        if(CommonUtil.isNotEmpty(ids)){
            for (Long id : ArrayUtil.stringToLongArray(ids, ",")) {
                Permission relation = new Permission();
                relation.setPrincipalId(roleId);
                relation.setResourceId(id);
                relation.setPrincipalType(PermPrincipalType.ROLE.getCode());
                this.permissionMapper.insert(relation);
            }
        }
    }

    @Override
    @Transactional(readOnly = false)
    public void delRoleById(Long roleId) {
        Wrapper<Role> d_wrapper = new EntityWrapper<>();
        d_wrapper = d_wrapper.like("path", "%[" + roleId + "]%");
        Integer d_ls = this.roleMapper.selectCount(d_wrapper);
        if (d_ls == null || d_ls > 0) {
            throw new BizException("角色下有子角色存在!");
        }
        // 删除角色
        this.roleMapper.deleteById(roleId);

        // 删除该角色所有的权限
        this.permissionMapper.deleteByPrincipalId(roleId);
    }

    @Override
    public List<Map<String, Object>> selectRoles(String condition) {
        return this.baseMapper.selectRoles(condition);
    }

    @Override
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.baseMapper.tree();
        tree.add(ZTreeNode.createRoot());
        return tree;
    }

    @Override
    public List<ZTreeNode> treeEveryone() {
        List<ZTreeNode> tree = this.baseMapper.treeEveryone();
        tree.add(ZTreeNode.createRoot());
        return tree;
    }

}
